//
//  LEEMainViewController.swift
//  01-weibo
//
//  Created by Captain-Lee on 16/4/7.
//  Copyright © 2016年 Capt-Lee. All rights reserved.
//

import UIKit

class LEEMainViewController: UITabBarController {

    
    // 点击了composedButton按钮
    @objc private func clickComposedButton() {
    
        print("点我了")
    }
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        addChildViewControllers()
        
        setupComposedButton()
    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        tabBar.bringSubviewToFront(composedButton)
    }
    
    // 懒加载控件
//    private lazy var composedButton: UIButton = {
//        // 自定义样式button
//        let btn = UIButton()
//        return btn
//    }()
    private lazy var composedButton: UIButton = UIButton(
        imageName: "tabbar_compose_icon_add",
        backImageName: "tabbar_compose_button")
    
}

// 主控制器
extension LEEMainViewController {
    
    
    // 设置撰写控制器
    private func setupComposedButton() {
    
        // 添加
        tabBar.addSubview(composedButton)
        
        // 调整按钮
        let count = childViewControllers.count
        // 让按钮宽一点点，能够解决手指触摸的容错问题
        let w = tabBar.bounds.width / CGFloat(count) - 1
        
        composedButton.frame = CGRectInset(tabBar.bounds, 2 * w, 0)
        
        composedButton.addTarget(self, action: "clickComposedButton", forControlEvents: .TouchUpInside)
    }
    
    

    // 添加所有的子控制
    private func addChildViewControllers() {
        
        // 设置 tintColor － 图片渲染颜色
        // 性能提升的技巧 - 如果能够用颜色解决，就不建议使用图片
//        tabBar.tintColor = UIColor.orangeColor()
        
        addChildViewController(LEEHomeViewController(), title: "首页", imageName: "tabbar_home")
        addChildViewController(LEEMessageViewController(), title: "消息", imageName: "tabbar_message_center")
        
        addChildViewController(UIViewController())

        addChildViewController(LEEDiscoverViewController(), title: "发现",
            imageName: "tabbar_discover")
        addChildViewController(LEEProfileViewController(), title: "我", imageName: "tabbar_profile")
        
    }
// 添加控制器
    private func addChildViewController(vc: UIViewController, title: String, imageName: String) {
    
        // 设置标题 － 由内至外设置的
        vc.title = title
        
        // 设置图片
        vc.tabBarItem.image = UIImage(named: imageName)
        
        // 设置导航控制器
        let nav = UINavigationController(rootViewController: vc)
        
        addChildViewController(nav)
    }

}




